package com.ha.service.aspect;

import com.ha.annotation.AutoFill;
import com.ha.annotation.AutoFillU;
import com.ha.constant.AutoFillConstant;
import com.ha.context.BaseContext;
import com.ha.context.BaseContext;
import com.ha.enumeration.OperationType;
import com.ha.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;

/**
 * ClassName: AutoFillAspect
 * Package: com.sky.service.aspect
 * Description:
 *自定义切面。实现公共字段自动填充的逻辑处理
 * @Author 那啥啥
 * @Create 2024/3/10  20:10
 * @Version 1.0
 */
@Aspect
@Component
@Slf4j
public class AutoFillAspect {
    //定义切入点

    /**
     * 定义切入点admin
     */
    @Pointcut("execution(* com.ha.mapper.*.*(..)) && @annotation(com.ha.annotation.AutoFill)")
    public  void autoFillPt(){}

    /**
     * User
     */
    @Pointcut("execution(* com.ha.mapper.*.*(..)) && @annotation(com.ha.annotation.AutoFillU)")
    public  void autoFillPtU(){}

    @Before("autoFillPt()")
    public  void autoFill(JoinPoint joinPoint){
        log.info("开始进行公共字段的自动填充...");
        //获取当前方法的操作类型
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();//方法签名对象
        AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);//获得方法的注解对象
        OperationType operationType = autoFill.value();//获取数据库访问的操作类型
//获取拦截方法的参数，实体对象
        Object[] args = joinPoint.getArgs();
        if(args == null || args.length == 0){
            return;
        }
        Object entity = args[0];
        //为实体对象公共属性统一准备，时间和操作人
        LocalDateTime now = LocalDateTime.now();
        Long currentId = BaseContext.getCurrentId();
        //通过反射赋值
        if(operationType == OperationType.INSERT){
            //公共字段赋值
            //获得set方法
            try {
                Method setCreateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method setCreateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                //反射赋值
                setCreateTime.invoke(entity,now);
                setCreateUser.invoke(entity,currentId);
                setUpdateTime.invoke(entity,now);
                setUpdateUser.invoke(entity,currentId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if(operationType == OperationType.UPDATE){
            try {
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                //反射赋值
                setUpdateTime.invoke(entity,now);
                setUpdateUser.invoke(entity,currentId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    @Before("autoFillPtU()")
    public  void autoFillU(JoinPoint joinPoint){
        log.info("开始进行公共字段的自动填充...");
        //获取当前方法的操作类型
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();//方法签名对象
        AutoFillU autoFillU = signature.getMethod().getAnnotation(AutoFillU.class);//获得方法的注解对象
        OperationType operationType = autoFillU.value();//获取数据库访问的操作类型
//获取拦截方法的参数，实体对象
        Object[] args = joinPoint.getArgs();
        if(args == null || args.length == 0){
            return;
        }
        Object entity = args[0];
        //为实体对象公共属性统一准备，时间和操作人
        LocalDateTime now = LocalDateTime.now();
        Long currentId = BaseContext.getCurrentId();
        //通过反射赋值
        if(operationType == OperationType.INSERT){
            //公共字段赋值
            //获得set方法
            try {
                Method setCreateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);

                //反射赋值
                setCreateTime.invoke(entity,now);
                setUpdateTime.invoke(entity,now);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if(operationType == OperationType.UPDATE){
            try {
                Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                //反射赋值
                setUpdateTime.invoke(entity,now);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

}
